<?php

/*
 * Create by Indonesian Brain Solutions
 * E-mail : cs@mesinit.com
 * Programmer : sony@sonywibisono.com
 */

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");

include("class/AppUsers.php"); 

_showMenuHeader();

function add(){
    $locked=  create_radio("locked", "Y;N", "Yes;No", "N");
    $active=  create_radio("active", "Y;N", "Yes;No", "N");
    $alert_receiver=  create_radio("sms_alert_receiver", "Y;N", "Yes;No", "N");
    $select_login=  select_login_type("social_media_type","");
    $select_role=  select_roles("app_role_id", "0");
    echo "<h2>Add Record</h2>
        <form action=\"?mod=account\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"># </td></tr>
<tr><td class=\"field_name\">User Name</td><td class=\"field_object\">
<input type=\"text\" name=\"username\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Pass Word</td><td class=\"field_object\">
<input type=\"text\" name=\"pass_word\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Full Name</td><td class=\"field_object\">
<input type=\"text\" name=\"full_name\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Email</td><td class=\"field_object\">
<input type=\"text\" name=\"email\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Mobile</td><td class=\"field_object\">
<input type=\"text\" name=\"mobile\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Active Session</td><td class=\"field_object\">
<input type=\"text\" name=\"active_session\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Locked</td><td class=\"field_object\">$locked </td></tr>
<tr><td class=\"field_name\">Active</td><td class=\"field_object\">$active </td></tr>
<tr><td class=\"field_name\">Sms Alert Receiver</td><td class=\"field_object\">
$alert_receiver </td></tr>
<tr><td class=\"field_name\">Social Media Login</td><td class=\"field_object\"><input type=\"text\" name=\"social_media_login\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Social Media Type</td><td class=\"field_object\">
$select_login</td></tr>
<tr><td class=\"field_name\">App Role</td><td class=\"field_object\">
$select_role </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function edit(){
    global $db,$id;
    if (!empty($id)){
    $sql="select * from app_users where id=$id";
    $row=$db->fetch_row($sql);
    extract($row);
    $lockeds=  create_radio("locked", "Y;N", "Yes;No", $locked);
    $actived=  create_radio("active", "Y;N", "Yes;No", $active);
    $alert_receiver=  create_radio("sms_alert_receiver", "Y;N", "Yes;No", $sms_alert_receiver);
    $select_login=  select_login_type("social_media_type",$social_media_type);
    $select_role=  select_roles("app_role_id", $app_role_id);
    echo "<h2>Update Record</h2>
        <form action=\"?mod=account\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\">#
<input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td class=\"field_name\">User Name</td><td class=\"field_object\">
<input type=\"text\" name=\"username\" class=\"textbox\" value=\"$user_name\"/> </td></tr>
<tr><td class=\"field_name\">Pass Word</td><td class=\"field_object\">
<input type=\"text\" name=\"pass_word\" class=\"textbox\" value=\"$pass_word\"/> </td></tr>
<tr><td class=\"field_name\">Full Name</td><td class=\"field_object\">
<input type=\"text\" name=\"full_name\" class=\"textbox\" value=\"$full_name\"/> </td></tr>
<tr><td class=\"field_name\">Email</td><td class=\"field_object\">
<input type=\"text\" name=\"email\" class=\"textbox\" value=\"$email\"/> </td></tr>
<tr><td class=\"field_name\">Mobile</td><td class=\"field_object\">
<input type=\"text\" name=\"mobile\" class=\"textbox\" value=\"$mobile\"/> </td></tr>
<tr><td class=\"field_name\">Active Session</td><td class=\"field_object\">
<input type=\"text\" name=\"active_session\" class=\"textbox\" value=\"$active_session\"/> </td></tr>
<tr><td class=\"field_name\">Locked</td><td class=\"field_object\">$lockeds </td></tr>
<tr><td class=\"field_name\">Active</td><td class=\"field_object\">$actived </td></tr>
<tr><td class=\"field_name\">Sms Alert Receiver</td><td class=\"field_object\">
$alert_receiver </td></tr>
<tr><td class=\"field_name\">Social Media Login</td><td class=\"field_object\"><input type=\"text\" name=\"social_media_login\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Social Media Type</td><td class=\"field_object\">
$select_login</td></tr>
<tr><td class=\"field_name\">App Role</td><td class=\"field_object\">
$select_role </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
    }
}
function add_process(){
    global $db,$id,$mod,$file,$username,$pass_word,$full_name,$email,$mobile,
            $active_session,$locked,$active,$sms_alert_receiver,
            $social_media_login,$social_media_type,$app_role_id;
    if (!empty($user_name)){
    //echo "Add Process Record";
    $app_user=new AppUsers();
    $app_user->user_name=$user_name;
    $app_user->pass_word=md5($pass_word);
    $app_user->full_name=$full_name;
    $app_user->email=$email;
    $app_user->mobile=$mobile;
    $app_user->locked=$locked;
    $app_user->sms_alert_receiver=$sms_alert_receiver;
    $app_user->social_media_type=$social_media_type;
    $app_user->social_media_login=$social_media_login;
    $app_user->active=$active;
    $app_user->app_role_id=$app_role_id;
    $app_user->insert();
    $url="?mod=$mod&file=$file";
    $msg="Add New Record Success";
    refresh_to($url, $msg, 2);
    }
}
function edit_process(){
    global $db,$id,$mod,$username,$pass_word,$full_name,$email,$mobile,
            $active_session,$locked,$active,$sms_alert_receiver,$social_media_login,$social_media_type,$app_role_id;
    if (!empty($id)){
    //echo "Add Process Record";
    $app_user=new AppUsers();
    $app_user->id=$id;
    $app_user->user_name=$username;
    $app_user->pass_word=md5($pass_word);
    $app_user->full_name=$full_name;
    $app_user->email=$email;
    $app_user->mobile=$mobile;
    $app_user->locked=$locked;
    $app_user->sms_alert_receiver=$sms_alert_receiver;
    $app_user->social_media_type=$social_media_type;
    $app_user->social_media_login=$social_media_login;
    $app_user->active=$active;
    $app_user->app_role_id=$app_role_id;
    $app_user->update();
    
    $url="?mod=$mod&action=list";
    $msg="Update Record Success";
    //refresh_to($url, $msg, 2);
    }
}
function delete_process(){
    global $db,$id,$mod,$file;
    if (!empty($id)){
    echo "Delete Process Record";
    $url="?mod=$mod&file=$file";
    $msg="Delete Record Success";
    $sql="delete from app_menus where id=$id";
    $db->query($sql);
    refresh_to($url, $msg, 2);
    }
}
function __default(){
    global $db,$page,$sortby,$sortorder,$mod,$pagesize,$file;
    if (empty($page)){
        $start=0;
        $sql="select a.*,b.role_name from app_users a,app_roles b
            where a.app_role_id=b.id order by user_name";
    } else {
        $start=$page*$pagesize;
        $sql="select a.*,b.role_name from app_users a,app_roles b
            where a.app_role_id=b.id order by user_name limit $start,$pagesize";
    }
   
    
    $query=$db->query($sql);
    echo "<h2>Application Menu List</h2>
        <div id=\"caption_table\"><a class='action_link' href=\"?mod=$mod&file=menus&action=add\">Add New</a></div>";
    echo "<div id=\"table_data\"><table width=\"100%\" cellpadding=2 cellspacing=0>
        <tr><th class='table_left_top'>Id</th><th>User Name</th><th>Full Name</th><th>E-mail</th>
        <th>SMS Alert</th><th>Locked</th><th>Login Type</th><th>Social Login</th>
        <th>Role</th>
        <th class='table_right_top'>Action</th></tr>";
    while ($row=$db->fetch_array($query)){
        extract($row);
        echo "<tr><td>$id</td><td>$user_name</td><td>$full_name</td><td>$email</td>
            <td align='center'>$sms_alert_receiver</td><td align='center'>$locked</td>
                <td>$social_media_type</td>
                <td>$social_media_login</td><td>$role_name</td>
                <td align=center><a href=\"?mod=$mod&file=$file&action=edit&id=$id\" class=\"icon_link\">
                    <img class='icon_small' src='images/edit.png' title='Edit'/></a>
        <a href=\"?mod=$mod&file=$file&action=delete&id=$id\" class=\"icon_link\">
            <img class='icon_small' src='images/delete.png' title='Delete'/></a></td></tr>";
    }
    echo "<tr class='row_odd'><td class=\"table_left_bottom\"></td>
        <td colspan='5'>&nbsp;</td><td class=\"table_right_bottom\"></td></tr>";
    echo "</table></div>";
}
global $action,$user_id,$login_status,$is_admin,$mod;
switch($action){
    case 'add':
        add();
        break;
    case 'edit':
        edit();
        break;
    case 'add_process':
        add_process();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete_process();
        break;
    case 'list':
    default:
        __default();
}
?>
